const { UPVOTE } = require("../config/error.js");
const {
    create,
    getUserMomentServer,
    getStepsServer,
    getLabelServer,
    upvoteServer,
    verifyUpvoteByUser,
    allMomentServer,
    momentByIdServer,
    addStepsServer,
    addStepsMany,
} = require("../server/momentServer.js");
const { addPhotoServer } = require("../server/uploadServer.js")

class MomentController {
    async create(ctx, next) {
        const { user_id, title, content, path_list } = ctx.request.body;
        const data = await create(user_id, title, content);
        if(path_list){
            const { insertId } = data;
            const imgData = path_list.map((item) => {
                return [insertId, user_id, item.path];
            });
            const data2 = await addPhotoServer(imgData);
            if(data && data2){
                ctx.body ={
                    code:0,
                    message:"发表动态成功",
                    data
                }
            }
        }
        if(data){
            ctx.body ={
                code:0,
                message:"发表动态成功",
                data
            }
        }
    }
    async getUserMoment(ctx, next) {
        const { home, user_id } = ctx.request.body;
        let data = await getUserMomentServer(user_id);
        if (home) {
            data = data.filter(
                (item) => item.id === 1 || item.id === 2 || item.id === 3
            );
            // 请求id为1005 的 前三个数据  固定
            ctx.body = {
                code: 0,
                message: "查询动态成功~",
                data,
            };
        } else {
            ctx.body = {
                code: 0,
                message: "查询动态成功~",
                data,
            };
        }
    }

    async getSteps(ctx, next) {
        const { momentId } = ctx.request.body;
        const data = await getStepsServer(momentId);
        ctx.body = {
            code: 0,
            message: "获取步骤成功",
            data,
        };
    }

    async addSteps(ctx, next) {
        const { moment_id, user_id, content } = ctx.request.body;
        const data = await addStepsServer(moment_id, user_id, content);
        ctx.body = {
            code: 0,
            message: "新增步骤成功~",
            data,
        };
    }

    async getLabel(ctx, next) {
        const data = await getLabelServer();
        ctx.body = {
            code: 0,
            message: "查询标签成功",
            data,
        };
    }

    async upvote(ctx, next) {
        if (ctx.upvote) {
            return ctx.app.emit("error", UPVOTE, ctx);
        }
        const { user_id, moment_id } = ctx.request.body;
        const data = await upvoteServer(user_id, moment_id);
        ctx.body = {
            code: 0,
            message: "点赞成功~",
            data,
        };
    }

    async allMoment(ctx, next) {
        const data = await allMomentServer();
        ctx.body = {
            code: 0,
            message: "获取所有动态~",
            data,
        };
    }

    async momentById(ctx, next) {
        const { momentId } = ctx.request.body;
        const data = await momentByIdServer(momentId);
        ctx.body = {
            code: 0,
            message: "获取指定动态成功~",
            data,
        };
    }
}

module.exports = new MomentController();
